package lyf.framework.web.servlet;

import lyf.framework.web.handler.HandlerManager;
import lyf.framework.web.handler.MappingHandler;

import javax.servlet.*;
import java.io.IOException;

public class DispatcherServlet implements Servlet {
	@Override
	public void init(ServletConfig config) throws ServletException {
		// 执行一些初始化工作
	}

	@Override
	public ServletConfig getServletConfig() {
		return null;
	}

	@Override
	public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
		doDispatch(req, res);
	}

	@Override
	public String getServletInfo() {
		return null;
	}

	@Override
	public void destroy() {

	}

	/**
	 * 对请求进行分发处理
	 */
	public void doDispatch(ServletRequest req, ServletResponse res) {

		for (MappingHandler mappingHandler : HandlerManager.mappingHandlerList) {
			try {
				if (mappingHandler.handle(req, res)) {
					return;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
